package com.comtrade.activities;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.comtrade.ilserver.tasks.UserRegistrationTask;
import com.comtrade.sharedpreferences.DomesticSharedPreferences;
import com.example.beaconapk.R;
import com.example.beaconapk.R.string;

public class RegisterActivity extends Activity {
	
	EditText username;
	EditText password;
	EditText firstName;
	EditText lastName;
	Button btnRegister;
	
	private UserRegistrationTask urt;
	private DomesticSharedPreferences sharedPreferences;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		sharedPreferences = new DomesticSharedPreferences();
		setTitle("Register");
		
		 username = (EditText)findViewById(R.id.username);
		 password = (EditText)findViewById(R.id.password);
		 firstName = (EditText)findViewById(R.id.first_name);
		 lastName = (EditText)findViewById(R.id.last_name);
		 btnRegister = (Button)findViewById(R.id.btn_register);
		 
		 btnRegister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				String userString = username.getText().toString();
				String passString = password.getText().toString();
				String firtString = firstName.getText().toString();
				String lastString = lastName.getText().toString();
				
				if (userString.trim().length() > 0 && userString != null &&
						passString.trim().length() > 0 && passString != null &&
						firtString.trim().length() > 0 && firtString != null &&
						lastString.trim().length() > 0 && lastString != null ){
					
					//ako su uneta sva polja
					
					if(passString.length() > 6){
						
						//ako password ima 6 karaktera
						//Slanje podataka na server
						
						urt = new UserRegistrationTask();
						
						urt.setUsername(userString);
						urt.setPassword(passString);
						urt.setFirstName(firtString);
						urt.setSurname(lastString);
						
						//Uzima url iz shared preferences
						String url = sharedPreferences.getPreferences(getApplicationContext(), "URL");						
						url += "/rest/register";
						
						//String url = "http://147.91.204.220:8080/ils/rest/register";
						
						
						urt.execute(url);
						
						try {
							String result;
							result = urt.get();
							
							if(result == null){
								Context context = getApplicationContext();
								CharSequence text = "Registration failed!";
								int duration = Toast.LENGTH_LONG;

								Toast toast = Toast.makeText(context, text, duration);
								toast.show();
							}
							else {
								
								//ako je uspesno
								
								Context context = getApplicationContext();
								CharSequence text = "Registration successful!";
								int duration = Toast.LENGTH_LONG;
								
								Toast toast = Toast.makeText(context, text, duration);
								toast.show();
																
								sharedPreferences.savePreferences(getApplicationContext(), "USERNAME", userString);
								sharedPreferences.savePreferences(getApplicationContext(), "PASSWORD", passString);
								
								Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
								startActivity(i);
								
								finish();
															
							}
							
							
							
						} catch (InterruptedException | ExecutionException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						
						
					}else {
						
						//ako password nema 6 karaktera
						Context context = getApplicationContext();
						CharSequence text = "Password must have minimum 6 characters!";
						int duration = Toast.LENGTH_LONG;

						Toast toast = Toast.makeText(context, text, duration);
						toast.show();
					}
					
					
				}else {
					//ako nisu uneta sva polja
					
					Context context = getApplicationContext();
					CharSequence text = "All fields must be filled!";
					int duration = Toast.LENGTH_LONG;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
					
				}
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		if (android.R.id.home==item.getItemId()) {
			Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
			startActivity(i);
			
		}
		return super.onOptionsItemSelected(item);
	}

}